async function getChannel() {
  // 获得频道
  const channel = await axios({ url: '/v1_0/channels' })
  console.log(channel);
  const Str = `<option value="" selected>请选择文章频道</option>` + channel.data.data.channels.map(item => `   <option value="${item.id}">${item.name}</option>`).join('')
  document.querySelector('.form-select').innerHTML = Str
}
getChannel()

// 文章封面设置
document.querySelector('.img-file').addEventListener('change', async e => {
  const img = e.target.files[0]
  const form = new FormData()
  form.append('image', img)
  const res = await axios({ url: '/v1_0/upload', method: 'POST', data: form })
  console.log(res);
  document.querySelector('.rounded').src = res.data.data.url
  document.querySelector('.rounded').classList.add('show')
  document.querySelector('.place').classList.add('hide')
})
// 重新选择图片
document.querySelector('.rounded').addEventListener('click', () => {
  document.querySelector('.img-file').click()
})
document.querySelector('.send').addEventListener('click', y = async (e) => {
  if (e.target.innerHTML !== '发布') return
  const form = document.querySelector('.art-form')
  const fo = serialize(form, { hash: true, empty: true })
  delete fo.id
  fo.cover = {
    type: 1,
    url: document.querySelector('.rounded').url
  }
  try {
    // 发布文章
    const public = await axios({
      url: '/v1_0/mp/articles',
      method: 'POST',
      data: fo
    })
    myAlert('true', '发布成功')
    document.querySelector('.rounded').src = ''
    document.querySelector('.rounded').classList.remove('show')
    document.querySelector('.place').classList.remove('hide')
    setTimeout(() => {
      location.href = '../content/content.html'
    })
  } catch (err) {
    myAlert(false, err.response.data.message)
  }
  form.reset()
})

  // 编辑功能
  ; (async function () {
    const paramsID = location.search
    const paramsURL = new URLSearchParams(paramsID)
    paramsURL.forEach(async (value, key) => {
      if (key === 'id') {
        document.querySelector('.title span').innerHTML = `修改文章`
        document.querySelector('.send').innerHTML = `修改`
        // 获取表单数据并回显
        const res = await axios({
          url: `/v1_0/mp/articles/${value}`
        })
        console.log(res);
        const obj = {
          channel_id: res.data.data.channel_id,
          title: res.data.data.title,
          rounded: res.data.data.cover.images[0],
          content: res.data.data.content,
          id: res.data.data.id
        }
        // 遍历数据对象属性，映射到页面元素上，快速赋值
        Object.keys(obj).forEach(key => {

          if (key === 'rounded') {
            if (obj[key]) {
              document.querySelector('.rounded').src = obj[key]
              document.querySelector('.rounded').classList.add('show')
              document.querySelector('.place').classList.add('hide')
            }
          } else if (key === 'content') {
            // 富文本内容
            editor.setHtml(obj[key])
          } else {
            document.querySelector(`#title`).value = obj.title
            document.querySelector(`#channel_id`).value = obj.channel_id
            document.querySelector(`[name=id]`).value = obj.id
          }
        })
      }
    })
  })()
document.querySelector('.send').addEventListener('click', y = async (e) => {
  if (e.target.innerHTML !== '修改') return
  // 修改文章编辑
  const form = document.querySelector('.art-form')
  const formStr = serialize(form, { hash: true, empty: true })
  console.log(formStr);
  try {
    const res = await axios({
      url: `/v1_0/mp/articles/${formStr.id}`,
      method: 'PUT',
      data: {
        ...formStr,
        cover: {
          type: document.querySelector('.rounded').src ? 1 : 0,
          images: [document.querySelector('.rounded').src]
        }
      }
    })
    myAlert(true, '修改成功')
  } catch (err) {
    myAlert(false, err.response.data.message)

  }
})
